<?php
class ControllerModuleReviews extends Controller {
	protected function index() {
		$this->language->load('module/reviews');

      	$this->data['heading_title'] = $this->language->get('heading_title');

		$this->load->model('catalog/product');
		$this->load->model('catalog/review');
		$this->load->model('catalog/reviewsrandom');
		$this->load->model('tool/seo_url');
		$this->load->model('tool/image');

		$this->data['reviews_random'] = array();

		$results = $this->model_catalog_reviewsrandom->getReviewsRandom($this->config->get('reviews_limit'));

		foreach ($results as $result) {
			if ($result['image']) {
				$image = $result['image'];
			} else {
				$image = 'no_image.jpg';
			}
			
			$rating = $this->model_catalog_review->getAverageRating($result['product_id']);

			$this->data['reviews_random'][] = array(
				'name'    => $result['name'],
   				'rating'  => $result['rating'],
				'text'    => strip_tags(substr($result['text'], 0, 80)),
        		'stars'   => sprintf($this->language->get('text_stars'), $result['rating']),
				'image'   => $this->model_tool_image->resize($image, 40, 40),
				'href'    => $this->model_tool_seo_url->rewrite(HTTPS_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id'])
			);
		}

		$this->id = 'reviews';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/reviews.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/reviews.tpl';
		} else {
			$this->template = 'default/template/module/reviews.tpl';
		}
		
		$this->render();
	}
}
?>